# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=lammps
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=20240829.2
_tag=stable_${pkgver:6:2}$(i=${pkgver:4:2}; LC_ALL=C date -d ${i}/01 +%b)${pkgver:0:4}
if [[ ${pkgver:9} != "" ]]; then
  _tag=${_tag}_update${pkgver:9}
fi
pkgrel=2
pkgdesc="Large-scale Atomic/Molecular Massively Parallel Simulator. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.lammps.org'
msys2_repository_url="https://github.com/lammps/lammps"
msys2_references=(
  'aur: lammps'
)
license=('spdx:GPL-2.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-charts")
             #"${MINGW_PACKAGE_PREFIX}-msmpi")
optdepends=("${MINGW_PACKAGE_PREFIX}-ffmpeg: Support generating movies"
            "${MINGW_PACKAGE_PREFIX}-qt6-charts: For lammps-gui")
source=("https://github.com/lammps/lammps/archive/${_tag}/${_realname}-${pkgver}.tar.gz"
        "001-undef-ABSOLUTE.patch"
        "002-fix-pkgconfig-file.patch"
        "003-fix-install-dll.patch"
        "004-fix-executables-extension.patch")
sha256sums=('f8ca3f021a819ced8658055f7750e235c51b4937ddb621cf1bd7bee08e0b6266'
            '2ab7d4e2158b953f1694ec955d993c461f151c4a6bded8edb576689d72b210a7'
            '320df126e7ca5a0c85eed2ca762e5a48a2c22c656571c0f054c514a0a7067c43'
            'b976476424badf991fc984df45780bf1a5ce2b01701626222d1c47bb34f3a705'
            '50ed7acd3e81a4af0e529e2c9d56a00e591feec44ff080ef177da0dfdf9081ec')
noextract=("${_realname}-${pkgver}.tar.gz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}"/${_realname}-${_tag}

  apply_patch_with_msg \
    001-undef-ABSOLUTE.patch \
    002-fix-pkgconfig-file.patch \
    003-fix-install-dll.patch \
    004-fix-executables-extension.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_OMP=ON \
      -DBUILD_MPI=OFF \
      -DBUILD_TOOLS=ON \
      -DBUILD_LAMMPS_GUI=ON \
      -DLAMMPS_EXCEPTIONS=ON \
      -DWITH_CURL=ON \
      -DWITH_FFMPEG=ON \
      -DWITH_GZIP=ON \
      -DWITH_JPEG=ON \
      -DWITH_PNG=ON \
      -DFFT=FFTW3 \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DENABLE_TESTING=OFF \
      -S "${_realname}-${_tag}"/cmake \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  cp build-${MSYSTEM}/lammps-gui.exe "${pkgdir}"${MINGW_PREFIX}/bin/

  install -Dm644 "${srcdir}"/${_realname}-${_tag}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
